package com.flowable.project.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiProcessSaveRequest;
import com.dingtalk.api.request.OapiProcessinstanceCreateRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiProcessSaveResponse;
import com.dingtalk.api.response.OapiProcessinstanceCreateResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.flowable.project.common.DingDingInfo;
import com.flowable.project.model.request.SendHttpModel;
import com.flowable.project.service.DingDingService;
import com.google.common.collect.Lists;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Auther: admin
 * @Date: 2021/11/20 11:01
 * @Description:
 */
@Service
public class DingDingServiceImpl implements DingDingService {

    @Override
    public String getToken() {
        DingTalkClient client = new DefaultDingTalkClient(DingDingInfo.TOKEN_URL);
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(DingDingInfo.APP_KEY);
        request.setAppsecret(DingDingInfo.APP_SECRET);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = null;
        try {
            response = client.execute(request);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return response.getAccessToken();
    }

    @Override
    public void saveTemplate(String token) {
        DingTalkClient client = new DefaultDingTalkClient(DingDingInfo.SAVE_TEMPLATE_URL);

        OapiProcessSaveRequest request = new OapiProcessSaveRequest();
        OapiProcessSaveRequest.SaveProcessRequest saveProcessRequest = new OapiProcessSaveRequest.SaveProcessRequest();
        saveProcessRequest.setDisableFormEdit(true);
        saveProcessRequest.setName("test2.0");
        saveProcessRequest.setProcessCode("PROC-BE7FC6B2-E95B-45CA-AD9A-A62819EDA2FE");
        saveProcessRequest.setAgentid(1371770442L);
        saveProcessRequest.setFakeMode(true);

// 注意，每种表单组件，对应的componentName是固定的，参照以下示例代码
        List<OapiProcessSaveRequest.FormComponentVo> formComponentList = Lists.newArrayList();

// 单行文本框
        OapiProcessSaveRequest.FormComponentVo singleInput = new OapiProcessSaveRequest.FormComponentVo();
        singleInput.setComponentName("TextField");
        OapiProcessSaveRequest.FormComponentPropVo singleInputProp = new OapiProcessSaveRequest.FormComponentPropVo();
        singleInputProp.setRequired(true);
        singleInputProp.setLabel("单行输入框");
        singleInputProp.setPlaceholder("请输入");
        singleInputProp.setId("TextField-J78F056R");
        singleInput.setProps(singleInputProp);
        formComponentList.add(singleInput);

// 多行文本框
        OapiProcessSaveRequest.FormComponentVo multipleInput = new OapiProcessSaveRequest.FormComponentVo();
        multipleInput.setComponentName("TextareaField");
        OapiProcessSaveRequest.FormComponentPropVo multipleInputProp = new OapiProcessSaveRequest.FormComponentPropVo();
        multipleInputProp.setRequired(true);
        multipleInputProp.setLabel("多行输入框");
        multipleInputProp.setPlaceholder("请输入");
        multipleInputProp.setId("TextareaField-J78F056S");
        multipleInput.setProps(multipleInputProp);
        formComponentList.add(multipleInput);

// 金额组件
        OapiProcessSaveRequest.FormComponentVo moneyComponent = new OapiProcessSaveRequest.FormComponentVo();
        moneyComponent.setComponentName("MoneyField");
        OapiProcessSaveRequest.FormComponentPropVo moneyComponentProp = new OapiProcessSaveRequest.FormComponentPropVo();
        moneyComponentProp.setRequired(true);
        moneyComponentProp.setLabel("金额（元）大写");
        moneyComponentProp.setPlaceholder("请输入");
        moneyComponentProp.setId("MoneyField-J78F0571");
        moneyComponentProp.setNotUpper("1"); // 是否禁用大写
        moneyComponent.setProps(moneyComponentProp);
        formComponentList.add(moneyComponent);

// 数字输入框
        OapiProcessSaveRequest.FormComponentVo numberComponent = new OapiProcessSaveRequest.FormComponentVo();
        numberComponent.setComponentName("NumberField");
        OapiProcessSaveRequest.FormComponentPropVo numberComponentProp = new OapiProcessSaveRequest.FormComponentPropVo();
        numberComponentProp.setRequired(true);
        numberComponentProp.setLabel("数字输入框带单位");
        numberComponentProp.setPlaceholder("请输入");
        numberComponentProp.setId("NumberField-J78F057N");
        numberComponentProp.setUnit("元");
        numberComponent.setProps(numberComponentProp);
        formComponentList.add(numberComponent);

// 日期
        OapiProcessSaveRequest.FormComponentVo dateComponent = new OapiProcessSaveRequest.FormComponentVo();
        dateComponent.setComponentName("DDDateField");
        OapiProcessSaveRequest.FormComponentPropVo dateComponentProp = new OapiProcessSaveRequest.FormComponentPropVo();
        dateComponentProp.setRequired(true);
        dateComponentProp.setLabel("日期");
        dateComponentProp.setPlaceholder("请选择");
        dateComponentProp.setUnit("小时"); // 小时或天
        dateComponentProp.setId("DDDateField-J8MTJZVE");
        dateComponent.setProps(dateComponentProp);
        formComponentList.add(dateComponent);

// 日期区间
        OapiProcessSaveRequest.FormComponentVo dateRangeComponent = new OapiProcessSaveRequest.FormComponentVo();
        dateRangeComponent.setComponentName("DDDateRangeField");
        OapiProcessSaveRequest.FormComponentPropVo dateRangeComponentProp = new OapiProcessSaveRequest.FormComponentPropVo();
        dateRangeComponentProp.setRequired(true);
        dateRangeComponentProp.setLabel(JSON.toJSONString(Arrays.asList("a", "b")));
        dateRangeComponentProp.setPlaceholder("请选择");
        dateRangeComponentProp.setUnit("小时"); // 小时或天
        dateRangeComponentProp.setId("DDDateRangeField-J78F057Q");
        dateRangeComponent.setProps(dateRangeComponentProp);
        formComponentList.add(dateRangeComponent);

        saveProcessRequest.setFormComponentList(formComponentList);
        request.setSaveProcessRequest(saveProcessRequest);

        OapiProcessSaveResponse response = null;
        try {
            response = client.execute(request, token);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(JSON.toJSONString(response));
    }

    @Override
    public String createProcess() {
        DingTalkClient client = new DefaultDingTalkClient(DingDingInfo.CREATE_PROCESS_URL);
        OapiProcessinstanceCreateRequest req = new OapiProcessinstanceCreateRequest();
        req.setAgentId(1371770442L);
        req.setProcessCode("PROC-DAE982B1-A80E-493F-B578-A8D53A644381");
        req.setOriginatorUserId("01241213026826225053");
        req.setDeptId(-1L);
//        req.setCcList("4525xxxxxxxx77041");
//        req.setCcPosition("FINISH");

        //单行输入框
        List<OapiProcessinstanceCreateRequest.FormComponentValueVo> formComponentValueVoList = new ArrayList<OapiProcessinstanceCreateRequest.FormComponentValueVo>();
        OapiProcessinstanceCreateRequest.FormComponentValueVo formComponentValueVo = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        formComponentValueVoList.add(formComponentValueVo);
        formComponentValueVo.setName("火灾地点");
        formComponentValueVo.setValue("杭电");






        OapiProcessinstanceCreateRequest.FormComponentValueVo formComponentValueVo1 = new OapiProcessinstanceCreateRequest.FormComponentValueVo();
        formComponentValueVoList.add(formComponentValueVo1);
        formComponentValueVo1.setName("火灾情况");
        formComponentValueVo1.setValue("大火");



        req.setFormComponentValues(formComponentValueVoList);
        OapiProcessinstanceCreateResponse rsp = null;
        try {
            rsp = client.execute(req, getToken());
        } catch (ApiException e) {
            e.printStackTrace();
        }
        System.out.println(JSON.toJSONString(rsp));
        return rsp.getProcessInstanceId();
    }

    /**
         * @Author zjl
         * @Description 根据用户id获取用户相关信息
         * @Date 16:30 2021/11/22
         * @Param [userId]
         * @return java.lang.String
         **/

    @Override
    public String getUserInfoById(String userId) {
        DingTalkClient client = new DefaultDingTalkClient(DingDingInfo.GET_USER_INFO_BY_ID_URL);
        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
        req.setUserid(userId);
        req.setLanguage("zh_CN");
        OapiV2UserGetResponse rsp = null;
        try {
            rsp = client.execute(req,getToken());
        } catch (ApiException e) {
            e.printStackTrace();
        }
        return rsp.getBody();
    }

    /**
         * @Author zjl
         * @Description 创建待办事件
         * @Date 17:08 2021/11/22
         * @Param
         * @return
         **/
    @Override
    public String createToDo() {
        return null;
    }
}
